from flask import Blueprint,request,jsonify
bp_course = Blueprint('course/',__name__)
from db import db1
import json

#接口
@bp_course.route('couponM/',methods=['POST','GET','PUT','DETELE'])
def couponM():
    #添加
    if request.method == 'POST':
        #获取用户输入
        data = request.data
        if data:
            data=json.loads(data)
        else:
            data = request.form
       #构造sql语句
        sql = "insert into coupon values(0,'%s',%d,'%s','%s')"%(data['name'],int(data['total']),data['stime'],data['etime'])
        #pymysql
        db1.insert(sql)
        #返回结果
        return 'ok'
    #获取一条数据
    elif request.method =='GET':
        id = request.args.get('id')
        sql = 'select * from coupon where id=%d'%(int(id))
        res = db1.find(sql)
        return jsonfiy({'code':200,'coupon':res})
    #修改数据
    elif request.method == 'PUT':
        #修改接口
        #获取用户输入
        data = request.data
        if data:
            data=json.loads(data)
        else:
            data = request.form
        print(data)
        #构造sql语句
        sql = 'update coupon set name="%s",total=%d,starttime="%s",endtime="%s" where id=%d'%(data['name'],int(data['total']),data['stime'],data['etime'],int(data['id']))
        print(sql)
        db1.insert(sql)
        return jsonify({'code':200})
    #删除接口
    elif request.method =='DELETE':
        #获取id
        id = request.args.get('id')
        sql = 'delete from coupon where id=%d'%(int(id))
        db1.insert(sql)
        return jsonfy({'code':200})

#部门表增删改查
@bp_course.route('/sectorM',methods=['POST','GET','PUT','DETELE'])
def sectorM():
    #添加
    if request.method == 'POST':
        #获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        #构造sql语句
        sql = 'insert into sector values(0,"%s","%s")'%(data['name'],int(data['number']))
        #pymysql
        db1.insert(sql)
        #返回结果
        return 'ok'
    #获取一条数据
    elif request.method == 'GET':
        #获取id
        id = request.args.get('id')
        sql = 'select * from sector where id=%d'%(int(id))
        res = db1.find(sql)
        return jsonify({'code':200,'coupon':res})
    #修改数据
    elif request.method == 'PUT':
        #获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        print(data)
        #构造sql语句
        sql = 'update sector set name="%s",number=%d where id=%d'%(data['name'],int(data['number']),int(data['id']))
        print(sql)
        db1.insert(sql)
        return jsonify({'code':200})
    #删除接口
    elif request.method =='DELETE':
        #获取id
        id = request.args.get('id')
        sql = 'delete from coupon where id=%d'%(int(id))
        db1.insert(sql)
        return jsonfy({'code':200})